local assets = {
	Asset("ANIM", "anim/musha_princesscrown.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_princesscrown.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_princesscrown.tex"),
	Asset("ANIM", "anim/musha_princesshat.zip")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.armor:SetAbsorption(0)
		return
	end
	inst.components.armor:SetAbsorption(
		math.max(inst.components.musha_equipment.currentlevel + 10, inst.components.musha_equipment.currentlevel * 2 - 10) *
			0.01
	)
	inst.components.equippable.dapperness =
		inst.components.musha_equipment.currentlevel < 20 and TUNING.DAPPERNESS_TINY or TUNING.DAPPERNESS_SMALL
	if display then
		inst.components.talker:Say(
			string.format(
				(inst.components.musha_equipment.currentlevel < 20 and STRINGS.MUSHA_HAT_PRINCESS or STRINGS.MUSHA_HAT_QUEEN) ..
					" (LV%d)\n" ..
						STRINGS.MUSHA_ITEM_SANITY_REGEN ..
							"(%s)\n" ..
								(inst.components.musha_equipment.currentlevel > 20 and STRINGS.MUSHA_ITEM_REGEN .. "(slow)\n" or "") ..
									STRINGS.MUSHA_ARMOR .. " (%d)\n",
				inst.components.musha_equipment.currentlevel,
				inst.components.musha_equipment.currentlevel < 20 and "Tiny" or "Small",
				math.max(inst.components.musha_equipment.currentlevel + 10, inst.components.musha_equipment.currentlevel * 2 - 10)
			)
		)
	end
end

local function OnHaunt(inst, haunter)
	if inst.cooldowntime ~= 0 or inst.components.musha_equipment.currentlevel < 10 then
		return false
	end

	inst.components.musha_equipment:DoDelta(-inst.components.musha_equipment:GetCurrent())
	inst.cooldowntime = 300
	inst:DoTaskInTime(
		inst.cooldowntime,
		function()
			inst.cooldowntime = 0
		end
	)

	SpawnPrefab("statue_transition").Transform:SetPosition(inst:GetPosition():Get())
	inst:DoTaskInTime(
		3,
		function()
			SpawnPrefab("statue_transition_2").Transform:SetPosition(inst:GetPosition():Get())
			SpawnPrefab("musha_glowdust").Transform:SetPosition(inst:GetPosition():Get())
			if not (inst.components.musha_equipment.currentlevel >= 20 and haunter:HasTag("_musha")) then
				inst:Remove()
			end
		end
	)
	return true
end

local function healowner(inst, owner)
	if inst.healing and not inst.broken then
		if (owner.components.health and owner.components.health:IsHurt()) then
			owner.components.health:DoDelta(1, false)
		end
	end
end

local function OnEquip(inst, owner)
	Updata(inst)

	if inst.components.musha_equipment.currentlevel < 20 then
		inst.components.equippable.dapperness = TUNING.DAPPERNESS_TINY
		owner.AnimState:OverrideSymbol("swap_hat", "musha_princesscrown", "swap_hat")
	else
		inst.healing = true
		owner.AnimState:OverrideSymbol("swap_hat", "musha_princesshat", "swap_hat")

		inst.components.equippable.dapperness = TUNING.DAPPERNESS_SMALL
		if inst.healing and not inst.broken then
			inst.heal =
				inst:DoPeriodicTask(
				15,
				function()
					healowner(inst, owner)
				end
			)
		end
	end

	owner.AnimState:Hide("HAIR")
	owner.AnimState:Hide("HAIR_NOHAT")
	owner.AnimState:Show("HAT_HAIR")
	owner.AnimState:Show("HAT")
end

local function StopShield(inst, data)
	if not (data.statename == "shell_idle" or data.statename == "shell_hit" or data.statename == "shell_enter") then
		inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD).components.useableitem:StartUsingItem()
	end
end

local function State_Change(inst)
	local owner = inst.components.inventoryitem.owner
	if not inst.on_shield then
		if inst.components.musha_equipment:IsBroken() then
			inst.components.talker:Say(STRINGS.MUSHA_ITEM_SHIELD_BROKEN .. "\n" .. STRINGS.MUSHA_ARMOR .. "(0)")
			inst.components.armor:SetAbsorption(0)
			return
		end

		inst.components.armor:SetAbsorption(1)
		inst.components.talker:Say(STRINGS.MUSHA_ITEM_SHIELD .. "\n" .. STRINGS.MUSHA_ARMOR .. "(100)")
		inst.components.musha_equipment:StartConsuming()

		if owner then
			owner.sg:GoToState("hide")
			owner.SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
		end
		inst:ListenForEvent("newstate", StopShield, owner)
		Updata(inst)
	else
		inst.components.musha_equipment:StopConsuming()
		inst.healing = false
		inst:RemoveEventCallback("newstate", StopShield, owner)
		owner.sg:GoToState("idle")
	end
	inst.on_shield = not inst.on_shield
	inst.components.useableitem:StopUsingItem()
end

local function OnUnequip(inst, owner)
	if inst.on_shield then
		State_Change(inst)
	end
	owner.AnimState:Hide("HAT")
	owner.AnimState:Hide("HAT_HAIR")
	owner.AnimState:Show("HAIR_NOHAT")
	owner.AnimState:Show("HAIR")
	if inst.consume then
		inst.consume:Cancel()
		inst.consume = nil
	end
	if inst.healowner then
		inst.healowner:Cancel()
		inst.healowner = nil
	end
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	inst.AnimState:SetBank("bushhat")
	inst.AnimState:SetBuild("musha_princesscrown")
	inst.AnimState:PlayAnimation("anim")

	inst:AddTag("metal")
	inst:AddTag("hide")

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		return inst
	end

	inst:AddComponent("useableitem")
	inst.components.useableitem:SetOnUseFn(State_Change)

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:InitMushaEquip("hat", 1050, 0.008)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			--[[ if inst.on_shield then
			State_Change(inst)
		end ]]
			if inst.components.armor.absorb_percent ~= 0 then
				inst.components.musha_equipment:StopConsuming()
				inst.components.armor:SetAbsorption(0)
				inst.components.equippable.walkspeedmult = 1
				inst.components.talker:Say(
					STRINGS.MUSHA_HAT_PRINCESS_BROKEN ..
						"\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)\n" .. STRINGS.MUSHA_ITEM_SPEED .. "(0)"
				)
			end
			inst.components.musha_equipment:StopConsuming()
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_princesscrown.xml"
	inst.components.inventoryitem.foleysound = "dontstarve/movement/foley/bushhat"

	----------resurrect
	inst:AddComponent("hauntable")
	inst.components.hauntable:SetHauntValue(TUNING.HAUNT_INSTANT_REZ)
	inst.components.hauntable:SetOnHauntFn(OnHaunt)

	inst:ListenForEvent(
		"activateresurrection",
		function(inst, guy)
			TheWorld:PushEvent("ms_sendlightningstrike", inst:GetPosition())
			inst.SoundEmitter:PlaySound("dontstarve/common/resurrectionstone_break")
			--OnHaunt(guy)
		end
	)

	inst.on_shield = false
	inst.type = "phoenix"

	inst.OnSave = function(inst, data)
		data.exp = inst.components.musha_equipment.currentexp
		data.cooldowntime = inst.cooldowntime
	end

	inst.OnLoad = function(inst, data)
		if data ~= nil then
			inst.cooldowntime = data.cooldowntime or 0
			inst:DoTaskInTime(
				inst.cooldowntime,
				function()
					inst.cooldowntime = 0
				end
			)
		end
	end

	return inst
end

return Prefab("musha_princesscrown", fn, assets)
